<!-- 车辆列表 -->
<template>
  <div class="dashboard-container vehicle customer-list-box">
    <div class="app-container">
      <!-- 头部搜索区域 -->
      <el-card
        class="search-card-box"
        shadow="never"
      >
        <el-form label-width="95px">
          <el-row :gutter="35">
            <el-col :span="8">
              <el-form-item
                label="车辆类型:"
                style="margin-bottom: 0px"
                prop="truckTypeId"
              >
                <el-select
                  v-model="CarTypeForm.truckTypeId"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="item in carTypeList"
                    :key="item.id"
                    ref="form"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item
                label="车牌号码:"
                style="margin-bottom: 0px"
                prop="licensePlate"
              >
                <el-input
                  v-model.trim="CarTypeForm.licensePlate"
                  clearable
                  placeholder="请输入车牌号码"
                />
              </el-form-item>
            </el-col>
            <el-col
              :span="8"
              style="padding-left: 40px"
            >
              <el-button
                type="warning"
                @click="vehicleFilter"
              >搜索</el-button>
              <el-button
                plain
                class="reset-btn"
                @click="resetForm"
              >重置</el-button>
            </el-col>
          </el-row>
        </el-form>
      </el-card>
      <!-- 中间tab栏切换区域 -->
      <el-card class="tab">
        <div class="tab-change">
          <div
            class="tab-item"
            :class="{active:isActive===1}"
            @click="go(1)"
          >
            <span class="status">全部</span>
            <span
              class="status-num"
              style="font-weight: bold"
            >{{ allList.length }}</span>
          </div>
          <div
            class="tab-item"
            :class="{active:isActive===2}"
            @click="go(2)"
          >
            <span class="status">可用</span>
            <span
              class="status-num"
              style="font-weight: bold"
            >{{ allList.filter(e=>e.workStatus===1).length }}</span>
          </div>
          <div
            class="tab-item"
            :class="{active:isActive===3}"
            @click="go(3)"
          >
            <span class="status">停用</span>
            <span
              class="status-num"
              style="font-weight: bold"
            >{{ allList.filter(e=>e.workStatus===0).length }}</span>
          </div>
        </div>
      </el-card>
      <!-- 底部列表渲染区域 -->
      <div
        v-loading="listLoading"
        element-loading-text="加载中"
        style="margin-top: 20px"
      >
        <el-card
          class="table-card-box"
          shadow="never"
        >
          <el-button
            type="warning"
            style="margin-bottom: 20px"
            @click="addCar"
          >新增车辆</el-button>
          <el-table
            :data="carVehicleList"
            fit
            style="width: 100%"
            stripe
            :header-cell-style="{ background: 'rgba(250,252,255,1)' }"
          >
            <!-- 没有数据时显示 -->
            <div
              v-show="
                (!carVehicleList || carVehicleList.length <= 0) &&
                  !listLoading"
              slot="empty"
            >
              <img
                src="@/icons/pic-kong.png"
                alt="img"
                style="margin-top: 20px; width: 25%; height: 25%"
              />
              <p style="margin-top: -20px; padding-bottom: 0px">这里空空如也</p>
            </div>
            <el-table-column
              align="left"
              label="序号"
              type="index"
              min-width="60px"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="车牌号码"
              prop="licensePlate"
              min-width="136px"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="车辆类型"
              prop="truckTypeName"
              min-width="131px"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="司机数量"
              prop="driverNum"
              min-width="131px"
            >
            </el-table-column>

            <el-table-column
              align="left"
              label="车辆状态"
              prop="status"
              min-width="131px"
            >
              <template #default="{row}">
                <span
                  class="point"
                  :class="{red:row.workStatus===0}"
                ></span>
                {{ formatStatus(row.workStatus) }}
              </template>
            </el-table-column>
            <el-table-column
              align="left"
              label="GPS设备ID"
              prop="deviceGpsId"
              min-width="197px"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="实载重量（T）"
              prop="allowableLoad"
              min-width="197px"
            >
            </el-table-column>
            <el-table-column
              align="left"
              label="实载体积（方）"
              prop="allowableVolume"
              min-width="197px"
            >
            </el-table-column>
            <!-- 操作按钮 -->
            <el-table-column
              align="center"
              label="操作"
              fixed="right"
              class-name="small-padding fixed-width"
              min-width="220px"
            >
              <template slot-scope="{ row }">
                <el-link
                  type="primary"
                  :underline="false"
                  @click="lookData(row.id)"
                >查看详细</el-link>
                <el-divider direction="vertical"></el-divider>
                <el-link
                  type="primary"
                  :underline="false"
                  :class="{red:row.workStatus==1}"
                  @click="handleWayllDetail(row,'switch')"
                >{{ row.workStatus=== 1 ? '停用' : '启用' }}</el-link>
                <el-divider direction="vertical"></el-divider>
                <el-link
                  type="primary"
                  :underline="false"
                  @click="handleWayllDetail(row,'edit')"
                >配置司机</el-link>
              </template>
            </el-table-column>
          </el-table>
          <!-- end -->
          <!-- 分页 -->
          <div
            class="pagination"
          >
            <div class="pages">
              <el-pagination
                :current-page="Number(CarTypeForm.page)"
                :total="Number(total)"
                :page-size="CarTypeForm.pageSize"
                :page-sizes="[10, 20, 30, 50]"
                layout="total, sizes, prev, pager, next, jumper"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
              />
            </div>
          </div>
        </el-card>
        <!-- 添加模态框 -->
        <vehicleAdd
          :dialog-form-visible.sync="dialogFormVisible"
          :car-type-list="carTypeList"
        ></vehicleAdd>
        <!-- 配置司机 -->
        <arrangeDriver
          ref="editCar"
          :title-info="titleInfo"
          :drivers-list="driversList"
          :car-modal-info="carModalInfo"
        ></arrangeDriver>
      </div>
    </div>
  </div>
</template>
<script>
import { truckList as getVehicleList, getCarTypeList, truckStatusUse, truckStatusNoUse, truckTypeDetail, getDriverListByCar } from '@/api/transit'
import vehicleAdd from './components/vehicle-add.vue'
import arrangeDriver from './components/arrange-driver.vue'
import { Message } from 'element-ui'
export default {
  name: 'Vehicle',
  components: {
    vehicleAdd,
    arrangeDriver
  },
  data() {
    return {
      isEditDialog: false,
      // 总条数
      total: null,
      // 车辆类型数组
      carTypeList: [],
      // 当页数据
      carVehicleList: [],
      // 总数据
      allList: [],
      // 配置司机回填数据
      carModalInfo: {},
      // 匹配司机列表
      driversList: [],
      // 匹配司机标题
      titleInfo: {
        text: '匹配司机'
      },
      // 加载状态
      listLoading: false,
      CarTypeForm: {
        page: 1,
        pageSize: 10,
        truckTypeId: null, // 车辆类型
        licensePlate: null, // 车牌号
        workStatus: null// 装态
      },
      // 动态类名
      isActive: 1,
      // 添加模态框显示隐藏
      isDialog: false,
      // 添加模态框显示隐藏
      dialogFormVisible: false
    }
  },
  async created() {
    // 获取车辆管理列表
    await this.getVehicleList()
    // 获取车辆类型数组
    await this.getCarTypeList()
    // 获取全部数据
    await this.getAllList()
  },
  methods: {
    /**
     * 关闭配置司机模态框
     */
    closeDailog() {
      this.isEditDialog = false
    },
    /**
     * 启停用按钮
     */
    handleWayllDetail(row, type) {
      const h = this.$createElement
      this.$msgbox({
        title: '车辆启用',
        message: h('p', null, [
          h('p', { style: 'margin-bottom: 5px' }, '确定要启用吗？车辆启用需满足以下条件： '),
          h('span', { style: 'margin-right: 40px' }, '1车辆信息已完善'),
          h('span', null, '2绑定司机>=2,且有排班')
        ]),
        showCancelButton: true,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        // 启用停用
        if (type === 'switch') {
          // 启用
          if (row.workStatus === 0) {
            const res = await truckStatusUse(row.id)
            if (res.code === 200) {
              Message.success('启用成功')
            } else {
              Message.error(res.msg)
            }
          }
          // 停用
          if (row.workStatus === 1) {
            const res = await truckStatusNoUse(row.id)
            if (res.code === 1) {
              Message.error(res.msg)
            } else {
              Message.success('停用成功')
            }
          }
        }
        // 司机配置
        if (type === 'edit') {
          const { data } = await truckTypeDetail(row.id)
          // 回填数据
          this.carModalInfo = data
          // 司机列表
          const res = await getDriverListByCar(row.id)
          this.driversList = res.data
          this.$refs.editCar.dialogFormVisible = true
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    /**
     * 添加车辆
     */
    addCar() {
      this.dialogFormVisible = true
    },
    /**
     * 查看详细跳转页
     */
    lookData(id) {
      this.$router.push({
        path: '/transit/vehicle-detail',
        query: {
          id,
          formUrlName: '车辆管理'
        }
      })
    },
    /**
     * 重置表单
     */
    resetForm() {
      this.CarTypeForm = {
        page: 1,
        pageSize: 10,
        truckTypeId: null, // 车辆类型
        licensePlate: null, // 车牌号
        workStatus: null// 装态
      }
      this.getVehicleList()
    },
    /**
     * 获取全部数据
     */
    async getAllList() {
      this.CarTypeForm.pageSize = Number(this.total)
      // 获取所有车辆列表
      const { data } = await getVehicleList(this.CarTypeForm)
      this.allList = data.items
      this.CarTypeForm.pageSize = 10
    },
    /**
     * 格式化状态信息
     */
    formatStatus(val) {
      if (val === 1) {
        return '正常'
      }
      if (val === 0) {
        return '禁用'
      }
    },
    /**
     * 搜索按钮点击事件
     */
    vehicleFilter() {
      this.CarTypeForm.page = 1
      // 将收集到的数据提交从新获取接口
      this.getVehicleList()
    },
    /**
     * tab栏切换点击
     */
    go(num) {
      this.isActive = num
      if (num === 1) {
        this.CarTypeForm.workStatus = null
        this.getVehicleList()
      }
      if (num === 2) {
        this.CarTypeForm.workStatus = 1
        this.getVehicleList()
      }
      if (num === 3) {
        this.CarTypeForm.workStatus = 0
        this.getVehicleList()
      }
    },
    /**
     * 切换页码
     */
    handleCurrentChange(val) {
      this.CarTypeForm.page = val
      // 获取车辆管理列表
      this.getVehicleList()
    },
    /**
     * 切换页容量
     */
    async handleSizeChange(val) {
      this.CarTypeForm.pageSize = val
      // 获取车辆管理列表
      this.getVehicleList()
    },
    /**
     * 获取车辆管理列表
     */
    async getVehicleList() {
      this.listLoading = true
      const { data } = await getVehicleList(this.CarTypeForm)
      this.total = data.counts
      this.carVehicleList = data.items
      this.listLoading = false
    },
    /**
     * 获取车辆类型列表
     */
    async getCarTypeList() {
      const { data } = await getCarTypeList()
      // 将数据赋值给车辆类型数组
      this.carTypeList = data
    }
  }
}
</script>
<style rel="stylesheet/scss" lang="scss" scoped>
.alert {
  margin: 10px 0px;
}
.pagination {
  margin-top: 10px;
}
.vehicle /deep/ .el-table td,
.el-table th {
  padding: 12px 0;
  min-width: 0;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  text-overflow: ellipsis;
  vertical-align: middle;
  position: relative;
  text-align: left;
  overflow: hidden;
}

.vehicle {
  /deep/ .el-dialog__title {
    width: 73px;
    height: 25px;
    margin-top: 30px;
    font-size: 18px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    text-align: left;
    color: #20232a;
    line-height: 25px;
    letter-spacing: 0px;
  }
  /deep/ .el-dialog__body {
    text-align: center;
    padding: 20px 20px 0px 20px;
  }
  .warn-img {
    width: 40px;
  }
  p {
    height: 24px;
    font-size: 16px;
    font-family: PingFangSC, PingFangSC-Regular;
    font-weight: 400;
    color: #818693;
    line-height: 24px;
  }
}

// 下拉框
/deep/ .el-select {
  width: 100%;
}
.el-input {
  width: 340px;
}
.el-link {
  font-size: 12px !important;

}
.el-link.red{
    color: red;
  }
// tab栏
.tab {
  margin-top: 20px;
  height: 48px;
 /deep/ .el-card__body {
    padding: 0 !important;
  }
  .tab-change {
    display: flex;
  }
  .tab-item {
    cursor:pointer;
    text-align: center;
    line-height: 48px;
    width: 120px;
    height: 48px;
    font-size: 14px;
  }
  .tab-item.active {
    background-color: #ffeeeb;
    color: #e15536;
    }
}
    // 状态处的点
   span.point{
    width: 6px;
    height: 6px;
    background: #1dc779;
    border-radius: 50%;
    display: inline-block;
    margin-right: 6px;
    }
    .point.red{
    background: #e15536;
    }
/deep/ .el-dialog__header{
height: 50px;
}
/deep/ .el-dialog__body{
  display: flex;
  flex-direction: column;
  align-items: center;
  .data-card{
  width: 540px;
  height: 130px;
  background: #fbfafa;
}
.el-card__body{
  display: flex;
  justify-content: space-around;

  p{
    margin: 5px;
    padding: 0;
    color: #000;
    font-size: 14px;
    span{
      color: #858a97;
    }
  }
  .bgc{
    color: #fff;
    background: #37c608;
    border-radius: 10px;
  }
}
/deep/ .el-form-item__content{
  margin-left: 0!important;
}
}

</style>

<style>
.startconfirm {
  width: 481px;
}
.startconfirm .el-message-box__header {
  padding-bottom: 4px;
}
.startconfirm .el-message-box__container {
  margin-bottom: 15px;
}
.confirm .el-message-box__status.el-icon-warning {
  left: 70px;
}
.confirm.el-message-box__title {
  font-size: 16px;
}
.confirm .el-message-box__message p {
  color: #f92d2d;
  margin-top: 5px;
}
.confirm .el-message-box__btns {
  text-align: center;
  margin-top: 10px;
}
</style>
